package com.htfyun.ft.myrecorder;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class RecorderOperator {
    private AudioRecord mRecord;
    // 音频获取源
    private int audioSource = MediaRecorder.AudioSource.MIC;

    // 设置音频采样率，44100是目前的标准，但是某些设备仍然支持22050，16000，11025
    private static int sampleRateInHz = 16000;
    // 设置音频的录制的声道CHANNEL_IN_STEREO为双声道，CHANNEL_CONFIGURATION_MONO为单声道
    private static int channelConfig = AudioFormat.CHANNEL_IN_STEREO;//AudioFormat.CHANNEL_CONFIGURATION_MONO;// AudioFormat.CHANNEL_IN_STEREO;
    // 音频数据格式:PCM 16位每个样本。保证设备支持。PCM 8位每个样本。不一定能得到设备支持。
    private static int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
    // 音频大小
    private int minBufSize;
    private String recordPath = "";
    private IRecordListener mIRecordListener;
    private volatile boolean isRecording = false;
    private static RecorderOperator instance;

    public static RecorderOperator getInstance() {
        if (null == instance) {
            synchronized (RecorderOperator.class) {
                if (null == instance) {
                    instance = new RecorderOperator();
                }

            }
        }
        return instance;
    }

    private RecorderOperator() {
    }

    private synchronized void initRecorder() {

        deInitRecorder();

        minBufSize = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig,
                audioFormat);
        mRecord = new AudioRecord(audioSource, sampleRateInHz, channelConfig,
                audioFormat, minBufSize);
    }

    private synchronized void deInitRecorder() {
        if (mRecord == null) {
            return;
        }

        mRecord.stop();
        mRecord.release();
        mRecord = null;

        isRecording = false;

    }

    public void setRecordPath(String path) {
        if (path != null && path.length() > 0) {
            recordPath = path;
        }
    }


    public void setRecordingListener(IRecordListener listener) {
        mIRecordListener = listener;
    }

    public void startRecord() {

        initRecorder();

        isRecording = true;
        //开启一个线程录音
        byte[] audiodata = new byte[minBufSize];
        mRecord.startRecording();
        if (mIRecordListener != null) {
            mIRecordListener.onRecordingStart();
        }
        int readsize = 0;
        FileOutputStream fos = null;
        if (recordPath != null && recordPath.length() > 0) {
            try {
                File file = new File(recordPath);
                if (file.exists()) {
                    file.delete();
                } else {
                    file.createNewFile();
                }
                fos = new FileOutputStream(file);// 建立一个可存取字节的文件
            } catch (Exception e) {
                e.printStackTrace();
                fos = null;
            }
        }

        while (isRecording) {
            readsize = mRecord.read(audiodata, 0, minBufSize);
            if (mIRecordListener != null) {
                mIRecordListener.onRecordingData(audiodata);
            }
//            Log.i("采集大小", String.valueOf(readsize));
            if (AudioRecord.ERROR_INVALID_OPERATION != readsize) {
                if (fos != null) {
                    try {
                        fos.write(audiodata);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        if (mIRecordListener != null) {
            mIRecordListener.onRecordingStop();
        }
        if (fos != null) {
            try {
                fos.close();// 关闭写入流
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        deInitRecorder();
    }

    public void stopRecord() {
        isRecording = false;

    }

    public static int getAudioFormat() {
        return audioFormat;
    }

    public static int getSampleRateInHz() {
        return sampleRateInHz;
    }

    public static int getChannelConfig() {
        return channelConfig;
    }
}

